<?php 
class Model{
	private static $link=NULL;
	
	private $table;
	
	private $opt=array();
	/**
	 * 构造，连接数据库
	 */
	public function __construct($table){
		if(is_null(self::$link)){
//			连接数据库
			$link = @new Mysqli(C('MYSQL_HOST'),C('MYSQL_USER'),C('MYSQL_POSSWORD'),C('MYSQL_DATABASE_NAME'));
			if($link->connect_errno){
				die($link->connect_error);
			}
			self::$link = $link;
			$sql = "SET NAMES " . C('MYSQL_CHARSET');
			$link->query($sql);
			$this->error($sql);
		}
		$this->table=$table;
		$this->opt();
	}
	private function opt(){
		$this->opt = array(
			'where' => '',
			'field' => '*',
			'order' => '',
			'limit' => '',
			'group' => '',
			'having'=> ''
			);
	}
	/**
	 * 删除
	 */
	public function delete(){
		//如果没有传递where条件
		if(!$this->opt['where']){
			header('Content-type:text/html;charset=utf-8');
			die('请必须传递WHERE条件才可以删除');
		}
		$sql = "DELETE FROM " . $this->table . $this->opt['where'];
		$this->exec($sql);
	}
	/**
	 * 查询  参数sql语句
	 */
	 public function query($sql){
	 	$result = self::$link->query($sql);
		$this->error($sql);
		$rows = array();
		while ($row = $result->fetch_assoc()) {
			$rows[] = $row;
		}
		$result->free();
		return $rows;
	 }
	 /**
	 * 增删改
	 */
	 public function exec($sql){
		self::$link->query($sql);
		//判断sql语句是否有问题
		$this->error($sql);
	}
	 /**
	 * 修改
	 */
	public function update($data=NULL){
		//如果没有传递where条件
		if(!$this->opt['where']){
			header('Content-type:text/html;charset=utf-8');
			die('请必须传递WHERE条件才可以修改');
		}
		//如果用户没有传递参数，获取$_POST
		if(is_null($data)) $data = $_POST;
		$info = '';
		foreach ($data as $field => $value) {
			$info .= $field . '="' . $value . '",';
		}
		$info = rtrim($info,',');
		$sql = "UPDATE " . $this->table . " SET {$info}" . $this->opt['where'];
		$this->exec($sql);
	}
	 /**
	 * 插入数据
	 */
	public function add($data=NULL){
		//如果用户没有传递参数，获取$_POST
		if(is_null($data)) $data = $_POST;
		//组合字段名
		$fields = implode(',',array_keys($data));
		//组合字段值
		$values = '"' . implode('","',array_values($data)) . '"';
		$sql = "INSERT INTO " . $this->table . "({$fields}) VALUES ({$values})";
		
		$this->exec($sql);
	}
	 /**
	 * 查询表 不用参数
	 */
	public function all(){
		$sql = "SELECT " . $this->opt['field'] . " FROM " . $this->table . $this->opt['where'] . $this->opt['group'] . $this->opt['having'] . $this->opt['order'] . $this->opt['limit'];
//		p($sql);
		return $this->query($sql);	
	}
	public function limit($limit){
		$this->opt['limit'] = " LIMIT {$limit}";
		return $this;
	}
	/**
	 * 排序
	 */
	public function order($order){
		$this->opt['order'] = " ORDER BY {$order}";
		return $this;
	}
	/**
	 * 传递查询条件 参数
	 */
	public function where($where){
		$this->opt['where'] = " WHERE $where ";
		return $this;
	}
	 /**
	  * 查询字段
	  */
	 public function field($field){
	 	$this->opt['field'] = $field;
		return $this;
	 }
	 public function group($group){
	 	$this->opt['group'] = " GROUP BY ".$group;
		return $this;
	 }
	/**
	 * 查询表 不用参数 只有一条数据使用find 返回一维数组
	 */
	public function find(){
		$data = $this->all();
		return current($data);
	}
	private function error($sql){
		//判断sql语句是否有问题
		if(self::$link->errno) 
			halt(self::$link->error . '</br>sql:' . $sql);
	}
}
